package net.akkz.action.feature;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.beanFactory.BeanFactory;
import net.akkz.model.Fulture;
import net.akkz.model.Student;
import net.akkz.server.FultureServer;

@WebServlet(name="FultureAdd", urlPatterns="/student/fultureAdd.do")
public class FultureAdd extends HttpServlet
{
	private FultureServer fultureServer;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		Student student = (Student)request.getSession().getAttribute("student");
		
		if(student == null)
		{
			response.sendRedirect("/login.html");
			return;
		}
		
		String message = request.getParameter("message");
		String date = request.getParameter("date");
		int studentId = student.getId();
		
		Fulture feature = new Fulture();
		feature.setDate(date);
		feature.setMessage(message);
		feature.setStudentid(studentId);
		feature.setJoined(1);
		
		fultureServer.addFulture(feature);
		
		response.sendRedirect("/fultures.html");
	}
	
	@Override
	public void init()
	{
		BeanFactory  bf = (BeanFactory) this.getServletContext().getAttribute("beanFactory");
		
		fultureServer = bf.getBean("fultureServer");
	}
}
